package com.example.service.impl;

import com.example.common.Page;
import com.example.common.R;
import com.example.domain.Article;
import com.example.mapper.ArticleMapper;
import com.example.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service

public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public void add(Article article) {
        article.setId(UUID.randomUUID().toString().replace("-",""));
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.add(article);
    }

    @Override
    public List<Article> page(Page<Article> page) {
        //设置正确的页数
        int pageNo = page.getPageNo() <= 0? 0:(page.getPageNo() - 1) * page.getPageSize();
        page.setPageNo(pageNo);
        if (page.getData() == null){
            page.setData(new Article());
        }
        return articleMapper.page(page);
    }

    @Override
    public int cunt(Page<Article> page) {
        return articleMapper.cunt(page);
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.update(article);
    }

    @Override
    public Article getOne(String id) {
        return articleMapper.getOne(id);
    }

    @Override
    public void del(String id) {
        articleMapper.del(id);
    }
}
